#include <stdio.h>
#include <msgQLib.h>
#include <taskLib.h>
#include <stdint.h>

#include "comm.h"
#include "tJournal.h"
#include "tExpedition.h"
#include "tMere.h"


void main()
{
	/*INIT_PACKET* p;
	char* b;
	int i;
	
	p = (INIT_PACKET*) malloc( sizeof(INIT_PACKET) );
	bzero( (char*) p, sizeof(INIT_PACKET) );
	p->hdr.id_packet = 0;
	p->id_lot = 128;
	p->nb_box_palette = 10;
	p->nb_defect_part = 26;
	p->nb_palette_number = 42;
	p->nb_part_box = 58;
	p->part_type = 1;
	
	b = (char*) p;
	
	for( i = 0; i < sizeof(INIT_PACKET); i++ )
	{
		printf( "0x%x - ", b[i] );
	}
	printf( " [%d bytes]\n", sizeof(INIT_PACKET) );
	
	free( p );*/
	
	ASK_COMMAND_PACKET packet;
	char buffer[PACKET_MAX_LENGTH];
	INIT_PACKET* pack;
	PACKET_TYPE type;
	int16_t i;
	
	semStock = semMCreate( SEM_Q_FIFO );
	taskSpawn( "tLog", 100, 0, 4096, (FUNCPTR) tJournal, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
	
	init_comm();
	
	type = recv_packet( TASK_MERE, buffer );
	pack = (INIT_PACKET*) buffer;
	
	for( i = 0; i < PACKET_MAX_LENGTH; i++ )
		printf( "0x%x | ", buffer[i] );
	printf( "\n" );
	printf( "%d - %d, %d, %d, %d, %d, %d\n", type, pack->id_lot, (pack->nb_box_palette), (pack->nb_defect_part), (pack->nb_palette_number), (pack->nb_part_box), (pack->part_type) );
	
	add_log( LOG_TYPE_STOP, "main", "" );
	
	semDelete( semStock );
	release_comm();
	
	/*
	START_PACKET p;
	int i = 0;
	
	taskSpawn( "tLog", 100, 0, 4096, (FUNCPTR) tJournal, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
	
	printf( "Init\n" );
	if( init_comm() == COMM_OK )
	{
		printf( "OK\n" );
	}
		
	p.hdr.id_packet = 'A';
	send_packet( (PACKET_HDR*) &p );
	
	printf( "Sent\n" );
	
	while( i < 3 )
	{
		char buffer[128];
		PACKET_HDR* p;
		PACKET_TYPE type;
		
		type = recv_packet( TASK_MERE, buffer );
		
		p = (PACKET_HDR*) buffer;
		
		if( type == PACKET_START )
		{
				printf( "Packet Start ! :: %d\n", p->id_packet );
				send_packet( p );
				i++;
		}
		if( type == PACKET_STOP )
		{
				printf( "Packet Stop ! :: %d\n", p->id_packet );
				send_packet( p );
				i++;
		}
	}
	
	{
		LOG_ENTRY log;
		log.type = LOG_TYPE_STOP;
		msgQSend( msg_log, (char*) &log, sizeof(log), WAIT_FOREVER, 0 );
	}
	
	printf( "Release\n" );
	release_comm();
	printf( "OK\n" );*/
}